<?php
/*
|
|--------------------------------------------------------------------------
| 产品列表页数据
|--------------------------------------------------------------------------
|
*/
include 'includes/common.php';
include 'ss-config.php';
include 'includes/functions.php';
include 'includes/share.php';

//包含左栏数据(产品导航 最新文章 联系方式)
include 'includes/leftdata.php';

//开始定义产品列表 根据传递到本页的catid值检索相应类别产品 如有子类则先输出子类导航
$catid = (isset($_GET['catid'])) ? $_GET['catid'] : 0;


//默认显示全部类别产品，如点击产品导航则显示该类别下产品
$query = sprintf('SELECT ID, CAT_ID, PRO_NAME, PRO_TYPE, PRO_PRICE, PRO_IMAGE FROM %sproducts WHERE IS_SHOW = 0 AND IS_DELETE = 0', DB_TBL_PREFIX);


//如果传递了类别参数
if ($catid != 0)
{
    //先定义面包屑中产品类别链接
    $cate_query = sprintf('SELECT FAT_ID, CATENAME, CATEDESC FROM %sproducts_cates WHERE ID = %d', DB_TBL_PREFIX, $catid);
    
    mysql_query("set names 'utf8'");
    $cate_result = mysql_query($cate_query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
    
    $cate_row = mysql_fetch_array($cate_result);
    $catefatid = $cate_row['FAT_ID'];  //获取当前类别的父类别ID
    $catename = $cate_row['CATENAME'];  //获取当前类别的名称
    $catedesc = $cate_row['CATEDESC'];  //获取当前类别的描述
    
    mysql_free_result($cate_result);
    
    //当前类别为顶级类别时仅在面包屑中显示类别名称
    $catelinks = '<a href="chanpin-' . $catid . '-1.html">' . $catename . '</a>';
    
    //如当前类别非顶级类别，则在面包屑中显示其各级上级类别链接
    if ($catefatid != 0) 
    {
        $cate_fat = sprintf('SELECT FAT_ID, CATENAME FROM %sproducts_cates WHERE ID = %d', DB_TBL_PREFIX, $catefatid);
    
        mysql_query("set names 'utf8'");
        $cate_fat_result = mysql_query($cate_fat, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
    
        $cate_fat_row = mysql_fetch_array($cate_fat_result);
        $cate_fat_fatid = $cate_fat_row['FAT_ID'];  //获取当前类别的父类别的父类别ID
        $cate_fat_name = $cate_fat_row['CATENAME'];  //获取当前类别的父类别名称
        
        mysql_free_result($cate_fat_result);
        
        //当前类别有上级类别时在面包屑中显示上级类别链接
        $catelinks = '<a href="chanpin-' . $catefatid . '-1.html">' . $cate_fat_name . '</a> - ' . '<a href="chanpin-' . $catid . '-1.html">' . $catename . '</a>';
        
        if ($cate_fat_fatid != 0)
        {
            $cate_fat_fat = sprintf('SELECT CATENAME FROM %sproducts_cates WHERE ID = %d', DB_TBL_PREFIX, $cate_fat_fatid);
    
            mysql_query("set names 'utf8'");
            $cate_fat_fat_result = mysql_query($cate_fat_fat, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
        
            $cate_fat_fat_row = mysql_fetch_array($cate_fat_fat_result);
            $cate_fat_fat_name = $cate_fat_fat_row['CATENAME'];  //获取当前类别的父类别的父类别的名称
            
            mysql_free_result($cate_fat_fat_result);
            
            //当上级类别又有上级类别时在面包屑中显示其上级类别链接
            $catelinks = '<a href="chanpin-' . $cate_fat_fatid . '-1.html">' . $cate_fat_fat_name . '</a> - <a href="chanpin-' . $catefatid . '-1.html">' . $cate_fat_name . '</a> - ' . '<a href="chanpin-' . $catid . '-1.html">' . $catename . '</a>';
        }
    }
    
    //在面包屑中加入各级类别链接
    $breadcrumb = ' - ' . $catelinks;
    
    //定义title和keywords
    // $unique_title = $catename . '系列_防盗井盖_复合井盖_铸铁井盖';
    if ($catid == 4)
    {
        //防盗井盖
        $unique_title = $catename . '_铸铁防盗井盖_防盗井盖厂家';
        $unique_keywords = '防盗井盖,铸铁防盗井盖,防盗井盖厂家';
    }
    elseif ($catid == 1)
    {
        //复合井盖
        $unique_title = $catename . '_复合材料井盖_复合井盖厂家';
        $unique_keywords = '复合井盖,复合材料井盖,复合井盖厂家';
    }
    elseif ($catid == 5)
    {
        //铸铁井盖
        $unique_title = $catename . '_球墨铸铁井盖_铸铁井盖规格';
        $unique_keywords = '铸铁井盖,球墨铸铁井盖,铸铁井盖规格';
    }
    elseif ($catid == 2)
    {
        //树脂井盖
        $unique_title = $catename . '_树脂井盖生产厂家_树脂井盖厂家';
        $unique_keywords = '树脂井盖,树脂井盖生产厂家,树脂井盖厂家';
    }
    elseif ($catid == 15)
    {
        //雨水篦子
        $unique_title = $catename . '_水篦子_雨水口篦子';
        $unique_keywords = '雨水篦子,水篦子,雨水口篦子';
    }
    elseif ($catid == 17)
    {
        //地沟篦子
        $unique_title = $catename . '_水篦子_雨水口篦子';
        $unique_keywords = '地沟篦子,水篦子,雨水口篦子';
    }
    elseif ($catid == 7)
    {
        //出水口
        $unique_title = $catename . '_灌溉出水口_浇地出水口';
        $unique_keywords = '出水口,灌溉出水口,浇地出水口';
    }
    elseif ($catid == 8)
    {
        //化粪池
        $unique_title = $catename . '_农村化粪池_新型化粪池';
        $unique_keywords = '化粪池,农村化粪池,新型化粪池';
    }
    else
    {
        $unique_title = $catename;
        $unique_keywords = $catename . ',防盗井盖,复合井盖,铸铁井盖';
    }

    // 定义description
    $unique_description = '北京井霸防盗井盖有限公司专业生产'.$catename.',';
    //剔除重复类别
    if ($catename == '防盗井盖' || $catename == '复合井盖' || $catename == '铸铁井盖' || $catename == '保温井盖' || $catename == '加锁井盖')
    {
        switch ($catename)
        {       
            case '防盗井盖': 
            $main_keywords = '复合井盖,铸铁井盖,保温井盖,加锁井盖';
            break;

            case '复合井盖': 
            $main_keywords = '防盗井盖,铸铁井盖,保温井盖,加锁井盖';
            break;

            case '铸铁井盖': 
            $main_keywords = '防盗井盖,复合井盖,保温井盖,加锁井盖';
            break;

            case '保温井盖': 
            $main_keywords = '防盗井盖,复合井盖,铸铁井盖,加锁井盖';
            break;

            case '加锁井盖': 
            $main_keywords = '防盗井盖,复合井盖,铸铁井盖,保温井盖';
            break;
        }

        $unique_description .= $main_keywords.'等井盖产品,生产工艺先进,产品质量可靠,咨询电话:'.$phone[1];
    }
    else
    {
        $unique_description .= '防盗井盖,复合井盖,铸铁井盖,保温井盖,加锁井盖等井盖产品,生产工艺先进,产品质量可靠,咨询电话:'.$phone[1];
    }
    
    
    //检查此类别是否包含下级分类，如包含下级分类则显示所有下级分类的产品
    $sub_cate = sprintf('SELECT ID, CATENAME FROM %sproducts_cates WHERE FAT_ID = %d', DB_TBL_PREFIX, $catid);
    
    mysql_query("set names 'utf8'");
    $sub_result = mysql_query($sub_cate, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
    
    if (mysql_num_rows($sub_result))  //如果此类别包含子类别
    {
        $sub_array = array();//包含该类别子类别ID和名称的数组
        
        while ($sub_row = mysql_fetch_array($sub_result))
        {
            $sub_array[] = array(
                                        'sub_cate_id' =>$sub_row['ID'],
                                        'sub_cate_name' =>$sub_row['CATENAME']
                                        ); 
        }
        
        foreach ($sub_array as $subcate)  //检查子类别是否又包含子类别
        {
            $sub_sub_cate = sprintf('SELECT ID,CATENAME FROM %sproducts_cates WHERE FAT_ID = %d', DB_TBL_PREFIX, $subcate['sub_cate_id']);
        
            mysql_query("set names 'utf8'");
            $sub_sub_result = mysql_query($sub_sub_cate, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
        
            if (mysql_num_rows($sub_sub_result))
            {
                //如某个子类别又包含子类别，则在数组中删除该子类别ID，因包含子类别的类别中不会包含产品数据
                $key = array_search($subcate['sub_cate_id'],$sub_array);
                unset($sub_array[$key]);
                
                while ($sub_sub_row = mysql_fetch_array($sub_sub_result))
                {
                    //将该类别的子类别的子类别ID添加到数组
                    $sub_array[] = array(
                                        'sub_cate_id' =>$sub_sub_row['ID'],
                                        'sub_cate_name' =>$sub_sub_row['CATENAME']
                                        );   
                }
            }
            
            mysql_free_result($sub_sub_result);
        }
        
        mysql_free_result($sub_result);
        
        //将包含该类别的 子类别 与 子类别的子类别 的ID的数组转换为以逗号分隔的字符串
        $sub_ids_data = array();
        foreach ($sub_array as $subcate_ids)
        {
            $sub_ids_data[] = $subcate_ids['sub_cate_id'];
        }
        
        $sub_ids = implode(',', $sub_ids_data);
        
        $query .= ' AND CAT_ID IN (' . $sub_ids . ')';
    }
    else
    {
        $query .= ' AND CAT_ID = ' . $catid;
    }
}


//如果执行搜索
if (isset($_REQUEST['search']))
{
    $keyword = mysql_real_escape_string($_REQUEST['search'], $GLOBALS['DB']);

    if ($keyword != '')
    {
        $query .= ' AND (PRO_NAME LIKE "%' . $keyword . '%" OR PRO_TYPE LIKE "%' . $keyword . '%")';
    }
    else
    {
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
        echo '<script>alert("请输入商品名称或型号!");history.back(-1);</script>';
    }
}

$query .= ' ORDER BY IS_TOP DESC, SUBMIT_DATE ASC';

//执行第一次查询
mysql_query("set names 'utf8'");
$result = mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));

$num = mysql_num_rows($result);  //获取记录总条数

$max = 20;  //设置每页记录数
$pagenum = ceil($num/$max);  //计算可分页数

if(!isset($_GET['page']) or !intval($_GET['page']) or !is_numeric($_GET['page']) or $_GET['page'] > $pagenum)
{
    $page = 1; //当页数不存在、不为十进制数、不是数字或大于可分页数时 值为1
}
else
{
    $page = $_GET['page'];  //接收当前页码数
}

$min = ($page-1)*$max;  //当前页从第$min条记录开始

mysql_free_result($result); //清除第一次查询结果集

$query .=  " limit $min,$max"; //定义显示当前页数据所需的查询语句

// 根据第一次查询的记录数判断，如有数据则输出
if ($num) 
{
    $product_result = mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));

    ob_start();
    
    //显示当前类别的各级子类别链接
    if(isset($sub_array))
    {
        $sublinks = '';
        
        foreach ($sub_array as $subcate_link)
        {
            $sublinks .= '<a href="product.php?catid=' . $subcate_link['sub_cate_id'] . '" class="subcatelink">' . $subcate_link['sub_cate_name'] . '</a>';
        }
        
        echo '<div class="subcatelinks">' . $catename . ': ' . $sublinks . '</div>';
    }
    
    // 显示类别描述
    if($catedesc != '')
    {
        echo '<div class="cate_desc">'.$catedesc.'</div>';
    }

    //产品列表
    echo '<ul class="product_ul">';
                           
    while ($product_row = mysql_fetch_array($product_result))
    {   
        /*  如果在总产品列表页chanpin.html点击产品，则定义包含产品类别拼音目录的链接；如果点击产品分类导航则不加拼音目录，因为.htaccess规则中已为每类产品定义了拼音目录，如此处再定义则重复，会导致目录错误，页面无法访问
        */
        $fromcatnav = (isset($_GET['procat'])) ? '1' : '0';
        
        if ($fromcatnav == '0')
        {
            //定义伪静态链接,包含该产品所属类别的拼音形式目录名
            switch ($product_row['CAT_ID'])
            {
                case '1': 
                $dirname = 'fuhejinggai';
                break;
                
                case '2': 
                $dirname = 'shuzhijinggai';
                break;
                
                case '3': 
                $dirname = 'guisujinggai';
                break;
                
                case '4': 
                $dirname = 'fangdaojinggai';
                break;
                
                case '5': 
                $dirname = 'zhutiejinggai';
                break;
                
                case '6': 
                $dirname = 'shuinijinggai';
                break;
                
                case '7': 
                $dirname = 'xiaonongshuichushuikou';
                break;
                
                case '8': 
                $dirname = 'huafenchi';
                break;
                
                case '9': 
                $dirname = 'jianchajingpeijian';
                break;
                
                case '10': 
                $dirname = 'jianchajinggai';
                break;
                
                case '11': 
                $dirname = 'lingmeijinggai';
                break;
                
                case '12': 
                $dirname = 'boligangjinggai';
                break;
                
                case '13': 
                $dirname = 'xiaofangjinggai';
                break;
                
                case '14': 
                $dirname = 'tongxinjinggai';
                break;
                
                case '15': 
                $dirname = 'yushuibizi';
                break;
                
                case '16': 
                $dirname = 'digougaiban';
                break;
                
                case '17': 
                $dirname = 'digoubizi';
                break;
                
                case '45': 
                $dirname = 'zhengtishuibiaojing';
                break;
            }
            
            //定义产品名称，此处定义产品名称是为了在总产品列表页防盗井盖的名称均显示为“防盗井盖”，而不是实际的“有齿锁舌”等不包含关键词的名称
            $productname = ($product_row['CAT_ID'] == '4') ? '防盗井盖' : $product_row['PRO_NAME'];
        
            echo '<li>';
            echo '<a href="' . $dirname . '/chanpin-view-' . $product_row['ID'] . '.html"><img src="' . $product_row['PRO_IMAGE'] . '" alt="' . $product_row['PRO_NAME'] . '"></a>';
            echo '<a href="' . $dirname . '/chanpin-view-' . $product_row['ID'] . '.html">' . $productname . '</a>';
            echo '</li>';
        }
        else
        {
            echo '<li>';
            echo '<a href="chanpin-view-' . $product_row['ID'] . '.html"><img src="' . $product_row['PRO_IMAGE'] . '" alt="' . $product_row['PRO_NAME'] . '"></a>';
            echo '<a href="chanpin-view-' . $product_row['ID'] . '.html">' . $product_row['PRO_NAME'] . '</a>';
            echo '</li>';
        }
    }

    echo '</ul>';
    
    mysql_free_result($product_result);

    //如果执行了搜索则在分页URL中传递搜索参数
    $pars = (isset($keyword)) ? '&search=' . $keyword : '';
    
    //如果选择了类别则在分页URL中传递类别参数
    if ($catid != 0)
    {
        $pars = '&catid=' . $catid;
    }
    
    //分页代码
    echo '<div class="page">';
    product_page($pars);
    echo '</div>';

    $products = ob_get_clean();
}
else
{
    $products = '<p class="nodata">无相关数据!</p>';
}


//调用产品列表页模板
include 'templates/chanpin.php';
?>